﻿#region Header

/*
Remote views based on linq
By

Pascal Hauser 
Dipl. Ing. in Informatik, Hochschule für Technik Rapperswil, 2006
Master Thesis, Hochschule für Technik Rapperswil, 2008-2010

and

Raphael Gfeller
Dipl. Ing. in Informatik, Hochschule für Technik Rapperswil, 2006
Master Thesis, Hochschule für Technik Rapperswil, 2008-2010

*/

#endregion

#region Usings

using System;
using System.Collections.Generic;
using System.Linq;

#endregion

namespace Sebastof.Services.Interfaces {
	/// <summary>
	/// Use a std. dictionary
	///  Impl of <see cref="IDataHandlingStrategy"/>
	/// </summary>
	public class StandardDataHandlingStrategy : IDataHandlingStrategy {
		#region Public Properties

		// public Properties located here

		#endregion

		#region Public Methods

		public StandardDataHandlingStrategy(object responsibleFor) {
			m_responsibleFor = responsibleFor;
		}

		#endregion

		#region Private Methods

		// private methods located here

		#endregion

		#region Private Fields

		private readonly Dictionary<string, Object> m_data = new Dictionary<string, object>();
		private readonly object m_responsibleFor;

		#endregion

		#region IDataHandlingStrategy Members

		public object GetValue(string key, object forObject) {
			return GetValue<Object>(key, forObject);
		}

		public T GetValue<T>(string key, object forObject) {
			if (m_data.ContainsKey(key)) {
				return (T)m_data[key];
			}
			//Logger.WriteDebug("could not find value for key:" + key + " and object " + forObject.ToString());
			return default(T);
		}

		public KeyValuePair<Object, KeyValuePair<string, object>[]>[] GetValues() {
			return new[] { new KeyValuePair<object, KeyValuePair<string, object>[]>(m_responsibleFor, m_data.ToArray()) };
		}

		public void SetValue(string key, object value, object forObject) {
			if (m_data.ContainsKey(key)) {
				m_data[key] = value;
			}
			else {
				m_data.Add(key, value);
			}
		}

		public bool IsDifferent(string key, object value, object forObject) {
			if (!m_data.ContainsKey(key)) {
				return false;
			}
			return (GetValue(key, forObject) == value);
		}

		#endregion
	}
}